﻿using System;
using System.Collections.Generic;
using System.Linq;
using App_Code.SiteParsers;
using PluginDB;
using App_Code.Helpers;

namespace App_Code.Rules
{
    /// <summary>
    /// Summary description for ExtractSeriesNameRule
    /// </summary>
    public class ExtractSeriesNameRule : IRule
    {
        public void ApplyRule(PluginVideoInfo pluginVideoInfo)
        {
            //If the movie is not of series type then exit
            if (!pluginVideoInfo.IsTVShow) return;

            string seriesName;
            int seasonNumber;
            int episodeNumber;            

            pluginVideoInfo.ExtractTVShowInfo(
                out seriesName,
                out seasonNumber,
                out episodeNumber);

            //If there is no session or episode name then its must be a movie
            if (seasonNumber == 0 && episodeNumber == 0)
            {
                pluginVideoInfo.IsTVShow = false;
                pluginVideoInfo.Categories.Clear();
                pluginVideoInfo.Categories.Add(pluginVideoInfo.SiteName);
                return;
            }

            pluginVideoInfo.IsTVShow = true;
            pluginVideoInfo.TVShowInformation.SeriesName = seriesName;
            pluginVideoInfo.TVShowInformation.SeasonNumber = seasonNumber;
            pluginVideoInfo.TVShowInformation.EpisodeNumber = episodeNumber == -1 ? 0 : episodeNumber;

            var pluginDBHandler = PluginDBHandler.Instance;
            if (!pluginDBHandler.IsSeriesExists(seriesName))
            {
                pluginVideoInfo.TVShowInformation.TVDBInformationItem = TheTVDBScraper.GetTVDBInformation(seriesName);
            }

            pluginVideoInfo.Categories.Clear();
            pluginVideoInfo.Categories.Add(seriesName);
        }
    }

}